/*
** interprete.c for 42sh in /home/demesy_j/svn/quarante-deuxsh/trunk
** 
** Made by julien demesy
** Login   <demesy_j@epitech.net>
** 
** Started on  Tue Mar 13 18:24:14 2012 julien demesy
** Last update Thu Mar 15 15:39:14 2012 julien demesy
*/

#include	<unistd.h>
#include	<stdlib.h>
#include	"parse.h"
#include	"execute.h"
#include	"list.h"

static int	get_size_to_malloc(char *cmd, int i)
{
  int		ret;

  while (cmd[i] && cmd[i] != ';')
    {
      ret++;
      i++;
    }
  return (ret);
}

static	void	free_tab(char **tab)
{
  int		i;

  if (tab)
    {
      i = 0;
      while (tab[i])
	if (tab[i])
	  free(tab[i++]);
      free(tab);
    }
}

void	interprete(char *cmd, t_list *env)
{
  char	*str;
  char	**tab;
  int	i;
  int	j;

  i = 0;
  while (cmd[i])
    {
      str = malloc((get_size_to_malloc(cmd, i) + 1) * sizeof(char));
      j = 0;
      while (cmd[i] != ';' && cmd[i])
	{
	  while (cmd[i] == ';')
	    i++;
	  str[j++] = cmd[i++];
	}
      str[j] = '\0';
      tab = decompose_simple(str);
      execute(tab, env);
      free_tab(tab);
      free(str);
      i++;
    }
}
